import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class GestureDetectorEx extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('GestureDetector 使用'),),
      body:DismissibleEx(),
      // ListView(
      //   children: [
      //     MyButton(),
      //     InkWellEx(),
      //   ],
      // ),
    );
  }
}

class MyButton extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        final snackbar = SnackBar(content: Text("Tap"));
        ScaffoldMessenger.of(context).showSnackBar(snackbar);
      },
      child: Container(
        padding: EdgeInsets.all(12.0),
        decoration: BoxDecoration(
          color: Theme.of(context).buttonColor,
          borderRadius: BorderRadius.circular(8.0),
        ),
        child: Text('Custom Button'),
      ),
    );
  }
}

//添加Material触摸水波效果
class InkWellEx extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: (){
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Tap')));
      },
      child: Container(
        padding: EdgeInsets.all(12.0),
        child: Text('Flat Button'),
      ),
    );
  }
}

//实现滑动关闭 Dismissible
class DismissibleEx extends StatelessWidget{
  final List<String> items = List<String>.generate(20, (index) => "Item ${index + 1}");

  @override
  Widget build(BuildContext context) {
    final title = 'Dismissing Items';
    return ListView.builder(itemBuilder: (BuildContext context, int index){
      final item = items[index];
      return Dismissible(
        key: Key(item),
        onDismissed: (DismissDirection direction){
          items.remove(index);
          ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('$item dismissed')));
        },
        background: Container(color: Colors.red,),
        child: ListTile(title: Text('$item'),),
      );
    },
      itemCount: items.length,
    );
  }
}